<?php
/*
客户跟进
*/
define('Acache_no','1');
class Con_meal_shop extends Acan{
	public $defaultAct='list';
	public $Conread = '餐馆管理';
	public $Actread = array('list'=>'所有餐馆','save'=>'保存','del'=>'删除','delall'=>'删除所有');
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){$this->id = '0';}
		$this->uid = Afun_uid();
		$this->model = Afun_db()->model(Acan_table_meal_shop);
		//print_r($this->model);
	}
	//--所有客户
	function Act_list(){
		$this->atitle = '所有餐馆';
		return $this->Act_my();
	}
	//--自己客户
	function Act_my(){
		if(empty($this->atitle)){$this->atitle = '我的记录';}
		$uid = $this->uid;
		if($this->id>1){
			$uid = $this->id;
		}
		$get = $_GET;
		$obj = Afun_acan_new('list');
		$key_name = $this->model->key_name;
		$columnrr = $this->model->comment;//初始栏目
		unset($columnrr[$key_name]);
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		$column_html = $obj->column_html($this->model,$column,'Aform');
		$column_json = $obj->form_json($this->model);
		//分页函数
		$con->select = "a.*";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $key_name;
		$page->order = "DESC";
		$page->page = '20';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->model->findAll($con);
		include $this->Atpl($this->Amod.'/my');
	}
	//--跟进客户
	function Act_customer(){
		$this->atitle = '跟进客户';
		$uid = $this->uid;
		if($this->id>1){
			$cid = $this->id;
			$_SESSION['track']['cid'] = $cid;
		}
		$obj = Afun_acan_new('list');
		$key_name = $this->model->key_name;
		$columnrr = $this->model->comment;//初始栏目
		unset($columnrr['cid'],$columnrr['uid']);
		$columnrr['username'] = '跟进人';
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		$column_html = $obj->column_html($this->model,$column,'Aform');
		$column_json = $obj->form_json($this->model);
		//分页函数
		$con->where = "a.cid=$cid";
		$con->select = "a.*,b.username";
		$con->join = "LEFT JOIN `".Acan_table_user."` b ON a.uid=b.uid";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $this->model->key_name;
		$page->order = "DESC";
		$page->page = '20';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->model->findAll($con);
		include $this->Atpl();
	}
	//--获取单用户信息
	function Act_info(){
		if($this->id<1){
			return;
		}
		$con->where = "a.id=".$this->id;
		$info = $this->model->ofind($con);
		Afun_json($info);
	}
	//批量更新
	function Act_save(){
		Afun_post_auth();
		$savedata = $_POST['save'];
		$savedata['uptime'] = Afun_get_time();
		if($this->id == 'one'){
			$key = $savedata[$this->model->key_name];
			unset($savedata[$this->model->key_name]);
			if(!empty($key)){
				$msg = $this->model->updatePk($savedata,$key);
			}else{//--添加一条记录
				$savedata['cid'] = $_SESSION['track']['cid'];
				$savedata['uid'] = $this->uid;
				$savedata['date'] = Afun_get_time();
				$msg = $this->model->save($savedata);
				$cmodel = Afun_db()->model(Acan_table_crm_customer);
				$cinfo = $cmodel->findPk($savedata['cid']);
				if($cinfo['active_time']==NULL || $cinfo['active_time']=="0000-00-00 00:00:00"){
					$customer['active_time'] = $savedata['date'];
				}
				$customer['last_time'] = $savedata['date'];
				/*if($savedata['outlook']>3){
					$customer['outlook'] = 1;
				}*/
				$cmodel->updatePk($customer,$savedata['cid']);//更新最后跟进时间
			}
		}elseif(is_array($savedata)){
			foreach($savedata as $i=>$vals){
				$this->model->updatePk($vals,$vals['key']);
			}
		}
		Afun_json($msg);
	}
	//删除
	function Act_del(){
		Afun_post_auth();
		if($this->model->deletePk($this->id)){
			echo '删除成功';
		}
	}
	//删除所选
	function Act_delall(){
		Afun_post_auth();
		foreach($_POST['chk'] as $key=>$val){
			if(empty($vala))
				$vala = "'$val'";
			else
				$vala .=",'$val'";
		}
		$where = " and source=".$this->uid;
		$this->model->delete($this->model->key_name." IN ($vala)".$where);
		Afun_refresh("删除成功",'referrer');
	}

}

?>